/**
 * @export 数字校验
 * @param {*} { min, max } 最小 最大
 * @return {*}
 */
export function validateNumSize ({ min, max }, message = '输入', callback = (e) => e) {
  return {
    validator (rule, value, callback = (e) => e) {
      const is_value = (
        ((value < min) && min !== 0) || ((value > max) && max !== 0)
      )
      if (is_value) {
        const mesg = `请${message}范围为${min}-${max}！`
        callback(mesg)
        return new Error(mesg)
      }
      callback()
    },
  }
}

/**
 * @export 小数保留校验
 * @param {*} { decimal_length 位数, message }
 * @return {*}
 */
export function validateDecimal ({ decimal_length, message }, callback = (e) => e) {
  return {
    validator (rule, value, callback = (e) => e) {
      const str = `^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,${decimal_length}})?$`
      const positiveReg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d{1,})$/
      const numReg = /^[0-9]\d*$/
      const reg = new RegExp(str)
      const val = value.toString()
      if (!val) return true

      if (!positiveReg.test(val)) {
        const mesg = `请${message}正数或整数！`
        callback(mesg)
        return new Error(mesg)
      }

      if (decimal_length === 0) {
        if (!numReg.test(val)) {
          const mesg = `请${message}正数或整数！`
          callback(mesg)
          return new Error(mesg)
        }
        callback()
        return true
      }

      if (!reg.test(val)) {
        const mesg = `请${message}保留${decimal_length}位小数！`
        callback(mesg)
        return new Error(mesg)
      }

      callback()
      return true
    },
  }
}
